package com.jeesite.modules.otherinbound.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.otherinbound.entity.OtherInboundHeader;
import com.jeesite.modules.otherinbound.dao.OtherInboundHeaderDao;
import com.jeesite.modules.otherinbound.entity.OtherInboundLines;
import com.jeesite.modules.otherinbound.dao.OtherInboundLinesDao;

/**
 * 其它入库单Service
 * @author 码脑洞
 * @version 2025-04-13
 */
@Service
public class OtherInboundHeaderService extends CrudService<OtherInboundHeaderDao, OtherInboundHeader> {
	
	@Autowired
	private OtherInboundLinesDao otherInboundLinesDao;
	
	/**
	 * 获取单条数据
	 * @param otherInboundHeader
	 * @return
	 */
	@Override
	public OtherInboundHeader get(OtherInboundHeader otherInboundHeader) {
		OtherInboundHeader entity = super.get(otherInboundHeader);
		if (entity != null){
			OtherInboundLines otherInboundLines = new OtherInboundLines(entity);
			otherInboundLines.setStatus(OtherInboundLines.STATUS_NORMAL);
			entity.setLinesList(otherInboundLinesDao.findList(otherInboundLines));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param otherInboundHeader 查询条件
	 * @param otherInboundHeader page 分页对象
	 * @return
	 */
	@Override
	public Page<OtherInboundHeader> findPage(OtherInboundHeader otherInboundHeader) {
		return super.findPage(otherInboundHeader);
	}
	
	/**
	 * 查询列表数据
	 * @param otherInboundHeader
	 * @return
	 */
	@Override
	public List<OtherInboundHeader> findList(OtherInboundHeader otherInboundHeader) {
		return super.findList(otherInboundHeader);
	}
	
	/**
	 * 查询子表分页数据
	 * @param otherInboundLines
	 * @param otherInboundLines page 分页对象
	 * @return
	 */
	public Page<OtherInboundLines> findSubPage(OtherInboundLines otherInboundLines) {
		Page<OtherInboundLines> page = otherInboundLines.getPage();
		page.setList(otherInboundLinesDao.findList(otherInboundLines));
		return page;
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param otherInboundHeader
	 */
	@Override
	@Transactional
	public void save(OtherInboundHeader otherInboundHeader) {
		super.save(otherInboundHeader);
		// 保存 OtherInboundHeader子表
		for (OtherInboundLines otherInboundLines : otherInboundHeader.getLinesList()){
			if (!OtherInboundLines.STATUS_DELETE.equals(otherInboundLines.getStatus())){
				otherInboundLines.setHeaderId(otherInboundHeader);
				if (otherInboundLines.getIsNewRecord()){
					otherInboundLinesDao.insert(otherInboundLines);
				}else{
					otherInboundLinesDao.update(otherInboundLines);
				}
			}else{
				otherInboundLinesDao.delete(otherInboundLines);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param otherInboundHeader
	 */
	@Override
	@Transactional
	public void updateStatus(OtherInboundHeader otherInboundHeader) {
		super.updateStatus(otherInboundHeader);
	}
	
	/**
	 * 删除数据
	 * @param otherInboundHeader
	 */
	@Override
	@Transactional
	public void delete(OtherInboundHeader otherInboundHeader) {
		super.delete(otherInboundHeader);
		OtherInboundLines otherInboundLines = new OtherInboundLines();
		otherInboundLines.setHeaderId(otherInboundHeader);
		otherInboundLinesDao.deleteByEntity(otherInboundLines);
	}
	
}